package com.wang.myapplication.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wang.myapplication.R;
import com.wang.myapplication.base.BaseActivity;
import com.wang.myapplication.base.MyApp;
import com.wang.myapplication.network.HttpRequest;
import com.wang.myapplication.network.bean.GlobalConfigResponse;
import com.wang.myapplication.network.bean.WildConfigResponse;
import com.wang.myapplication.tools.SharedPreTool;
import com.wang.myapplication.tools.StringTool;

import java.util.HashMap;
import java.util.List;

public class CompanyLoginActivity extends BaseActivity {

    private EditText etCompanyId;
    private Button btnLogin;
    private GlobalConfigResponse globalConfig;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public void initParams(Bundle params) {
        //已经有了配置文件就自动跳过
        if (SharedPreTool.getValue(SharedPreTool.KEY_GLOBALCONFIG, SharedPreTool.STRING) != null && SharedPreTool.getValue(SharedPreTool.KEY_WILDCONFIG, SharedPreTool.STRING) != null) {
            //取出来配置
            SharedPreTool.getValue(SharedPreTool.KEY_GLOBALCONFIG, SharedPreTool.STRING);
            String wildconfig = (String) SharedPreTool.getValue(SharedPreTool.KEY_WILDCONFIG, SharedPreTool.STRING);
            WildConfigResponse wildConfigResponse = new Gson().fromJson(wildconfig, WildConfigResponse.class);
            $Log("SharedPreTool取出:" + wildConfigResponse);
            MyApp.getMyApp().setWildConfigResponse(wildConfigResponse);
            startActivity(UserLoginActivity.class);
            this.finish();
            return;
        }
        reqGlobalConfig();

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_company_login;
    }

    @Override
    public void initView(Bundle savedInstanceState, View view) {
        etCompanyId = findViewById(R.id.etCompanyId);
        btnLogin = findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(this);
    }


    @Override
    public void doBusiness(Context mContext) {
    }

    @Override
    public void widgetClick(View v) {
        if (!StringTool.isInputValid(etCompanyId.getText())) {
            Toast.makeText(this, "请输入企业号", Toast.LENGTH_SHORT).show();
            return;
        }

        String inputCompanyId = etCompanyId.getText() + "";
        if (checkValidCompanyID(inputCompanyId)) {
            String configURL = MyApp.getMyApp().getCompanys().getConfigurl();
            reqWildConfig(configURL);
        } else
            Toast.makeText(this, "请输入正确的ID" + globalConfig.getConfigversion(), Toast.LENGTH_SHORT).show();

    }

    private boolean checkValidCompanyID(String inputCompanyId) {
        //取出配置 对比有效性，获取wildConfig配置保存，登录
        globalConfig = MyApp.getMyApp().getGlobalConfigResponse();
        List<GlobalConfigResponse.Companys> companys = globalConfig.getCompanys();
        for (int i = 0; i < companys.size(); i++) {
            if (companys.get(i).getCompanyid().equals(inputCompanyId)) {
                //
                GlobalConfigResponse.Companys curCompany = companys.get(i);
                MyApp.getMyApp().setCompanys(curCompany);
                return true;
            }
        }

        return false;
    }

    private void reqGlobalConfig() {
        HttpRequest.reqGlobalConfig(new MyHttpback<GlobalConfigResponse>() {
            @Override
            public void onSuccessBack(GlobalConfigResponse obj) {
                SharedPreTool.putValue(SharedPreTool.KEY_GLOBALCONFIG, obj);
                MyApp.getMyApp().setGlobalConfigResponse(obj);
                //设置配置
            }
        });
    }

    private void reqWildConfig(String configURL) {
        HashMap<String, Object> params = new HashMap<>();
        HttpRequest.reqWildConfig(new MyHttpback<WildConfigResponse>() {
            @Override
            public void onSuccessBack(WildConfigResponse obj) {
                Toast.makeText(CompanyLoginActivity.this, "" + obj.getCompanyname(), Toast.LENGTH_SHORT).show();
                SharedPreTool.putValue(SharedPreTool.KEY_WILDCONFIG, obj);
                MyApp.getMyApp().setWildConfigResponse(obj);
                startActivity(UserLoginActivity.class);
                CompanyLoginActivity.this.finish();
            }
        }, configURL);
    }


}
